import sys
import logging  # 确保导入logging
from system import TradingSystem
from PyQt5.QtWidgets import QApplication
from trading_ui import TradingSystemUI  # 确保这行存在

if __name__ == "__main__":
    # 配置日志
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.StreamHandler(),
            logging.FileHandler("trading_system.log")
        ]
    )

    logger = logging.getLogger(__name__)
    logger.info("启动股票跟单系统")
    # 创建应用
    app = QApplication(sys.argv)

    try:
        # 创建交易系统
        logger.info("创建交易系统实例")
        system = TradingSystem("stock_follow.db")
        logger.info("交易系统创建成功")

        # 创建UI
        logger.info("创建用户界面")
        ui = TradingSystemUI(system)
        ui.show()
        logger.info("用户界面已显示")

        # 不自动启动监控，等待用户点击"开始监控"按钮
        # 启动监控
        #system.monitor.start_monitoring()

        # 执行应用
        sys.exit(app.exec_())

    except Exception as e:
        logger.error(f"系统启动失败: {str(e)}")
        sys.exit(1)